import {waterSign, getCaiyuanInfo,waterVagetable, changePointsToWater} from 'config/api'
import {Toast} from 'antd-mobile'
import {delay} from 'dva/saga'
export default {
  namespace: 'zhongcai',
  state:{
    water: 0,
    water_has_eat: 0,
    vagetable: 0,
    points: 0,
    headimgurl: '',
    showJiaoshui: false
  },
  effects:{
    *waterSignEff({callback}, {call,put}){
      let {code,msg} = yield call(waterSign)
      if(code === 0){
        callback()
        yield put({
          type: 'getCaiyuanInfoEff'
        })
      }else{
        Toast.fail(msg)
      }
    },
    *waterVagetableEff(action, {call, put}){
      let {code,msg} = yield call(waterVagetable)
      if(code === 0){

        yield put({
          type: 'getCaiyuanInfoEff'
        })
        yield put({
          type: 'save',
          payload: {showJiaoshui: true}
        })
        yield call(delay, 2000)
        yield put({
          type: 'save',
          payload: {showJiaoshui: false}
        })
        Toast.success(msg)
      }else{
        Toast.info(msg)
      }
    },
    *getCaiyuanInfoEff(action, { call, put }) {
      let response = yield call(getCaiyuanInfo);
      yield put({
        type: 'save',
        payload: response
      })
    },
    *changePointsToWaterEff({pointsToChange}, {call, put}){
      let {code,msg} = yield call(changePointsToWater, {pointsToChange})
      if(code === 0){
        Toast.success(msg)
        yield put({
          type: 'getCaiyuanInfoEff'
        })
      }else{
        Toast.info(msg)
      }
    },
  },
  reducers:{
    save(state,{payload}){
      return {...state,...payload}
    }
  }
}
